//
//  AppDelegate.swift
//  LBNewWeiBo
//
//  Created by bin on 15/10/26.
//  Copyright © 2015年 bin. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
       
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.rootViewController = defaultController()
        
        window?.makeKeyAndVisible() // 启动窗口
        
       self.setupAppearance()
        
        return true
        
    }

    /// 默认选择的控制器
    private func defaultController() -> UIViewController {
        // 判断是否登录
        if !LBUserAccout.userLogin() {
            return LBMainViewController()
        }
        
        // 判断是否新版本
        return isNewView() ? LBNewFeatureCollection() : LBWelcomViewController()
    }
    
    private func isNewView() ->Bool {
        // 获取当前的版本号
        let versionStr = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let currentVersion = Double(versionStr)! // 当前版本
        
        // 获取之前的版本号
        let sandboxVersionkey = "sandboxVersionkey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionkey)
        // 保存当前版本号
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionkey)
        // 同步
        NSUserDefaults.standardUserDefaults().synchronize()
        
        return currentVersion > sandboxVersion // 当前 > 之前
    }
    
//MARK: - 切换根控制器
    func switchRootController(isMain: Bool) {
        window?.rootViewController = isMain ? LBMainViewController() : LBWelcomViewController()
    }
    
    
/// MARK: -  设置应用全局的导航title 字体颜色 setupAppearance
    /// true :main 主页 -- false welcome 欢迎界面
    private func setupAppearance(){
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
    
}

